import { requestClient } from '#/api/request';

export namespace DictApi {
  /**
   * 字典项
   */
  export interface SelectOption {
    label: string;
    value: string;
  }
  /**
   * 字典项对象
   */
  export interface SelectOptionObject {
    [key: string]: SelectOption[];
  }
  export type Params = object;
  /**
   * 字典项
   */
  export interface Datum {
    searchValue: null;
    createBy: string;
    createTime: Date;
    updateBy: null;
    updateTime: null;
    remark: string;
    params: Params;
    dictCode: number;
    dictSort: number;
    dictLabel: string;
    dictValue: string;
    dictType: string;
    cssClass: string;
    listClass: string;
    isDefault: string;
    status: string;
    default: boolean;
  }
  /**
   * 字典响应
   */
  export interface DictResp {
    msg: string;
    code: number;
    data: Datum[];
  }
}

/**
 * 根据字典值获取字典标签
 * @param {string[]} params 字典值
 * @returns {Promise<DictApi.SelectOptionObject>} 字典标签
 */
export const DictResp = async (params: string[]) => {
  const res: Promise<DictApi.DictResp>[] = [];
  const result: DictApi.SelectOptionObject = {};
  for (const param of params) {
    const resp = requestClient.get<DictApi.DictResp>(
      `/system/dict/data/type/${param}`,
    );
    res.push(resp);
  }
  await Promise.all(res).then((res1) => {
    for (const [i, element] of res1.entries()) {
      const arr: DictApi.SelectOption[] = [];
      element?.data.forEach((item) => {
        arr.push({
          label: item.dictLabel,
          value: item.dictValue,
        });
      });
      // 确保 params[i] 不是 undefined
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
      result[params[i]!] = arr;
    }
  });
  return result;
};

/**
 * 获取保养类型字典
 * @returns {Promise<DictApi.DictResp>} 字典响应
 */
export const mesMaintenType = () =>
  requestClient.get<DictApi.DictResp>(
    '/system/dict/data/type/mes_mainten_type',
  );
